package tree.test404;

import listbyorder.utils.TreeNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/18 8:45
 */
public class Solution1 {

    // Morris遍历
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        TreeNode c1 = root;
        TreeNode c2;
        int ans = 0;
        while (c1 != null) {
            c2 = c1.left;
            if (c2 != null) {
                while (c2.right != null && c2.right != c1) {
                    c2 = c2.right;
                }
                if (c2.right == null) {
                    c2.right = c1;
                    c1 = c1.left;
                    continue;
                }
                if (c2.right == c1) {
                    if (c1.left == c2 && c2.left == null) {
                        ans += c2.val;
                    }
                    c2.right = null;
                }
            }
            c1 = c1.right;
        }
        return ans;
    }
}
